package com.jy.appmarket.dialog;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jy.appmarket.R;

/**
 * 类说明： 用法：先调用 {@link #OptionDialog(Context, String, String[])} 进行初始化，然后调用
 * {@link #setOptionsItemClickListener(OnOptionsItemClickListener)}设置监听。
 * 
 * @author xinhui.cheng
 * @date 2013-4-7
 * @version 1.0
 */
public class OptionDialog extends Dialog {

	private Context mContext;
	private String[] mOptions;
	private String mTitle;
	private TextView mTitleTv;
	private ListView mListView;

	public OptionDialog(Context context, String title, String[] options) {
		super(context, R.style.custom_dialog_theme);
		mContext = context;
		mTitle = title;
		mOptions = options;
		init();
		loadData();
	}

	private void init() {
		setContentView(R.layout.options_dialog_layout);
		mTitleTv = findViewById(R.id.options_dialog_title_tv);
		mListView = findViewById(R.id.options_dialog_list);
	}

	private void loadData() {
		if (mOptions == null || mOptions.length == 0) {
			return;
		}
		ArrayList<String> optionsList = new ArrayList<String>();
		for (String op : mOptions) {
			optionsList.add(op);
		}
		mTitleTv.setText(mTitle);
		mListView.setAdapter(new OptionsDialogAdapter(mContext, optionsList));
	}

	/**
	 * 设置监听
	 * 
	 * @param listener
	 * @return
	 */
	public OptionDialog setOptionsItemClickListener(OnItemClickListener listener) {
		mListView.setOnItemClickListener(listener);
		return this;
	}

	@Override
	public boolean dispatchKeyEvent(android.view.KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER
				|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN
				|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP
				|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT
				|| event.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
			return true;
		}

		return super.dispatchKeyEvent(event);

	}

	private static class OptionsDialogAdapter extends BaseAdapter {
		private Context mContext;
		private List<String> mList;

		public OptionsDialogAdapter(Context context, List<String> list) {
			mContext = context;
			mList = list;
		}

		@Override
		public int getCount() {
			return mList == null ? 0 : mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(mContext).inflate(
						R.layout.options_dialog_item_layout, parent, false);
				holder.optionTv = convertView
						.findViewById(R.id.options_dialog_item_option_tv);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.optionTv.setText(mList.get(position));
			return convertView;
		}

		private static class ViewHolder {
			TextView optionTv;
		}

	}

}
